package com.hxcy.doki;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rl_home_container)
    RelativeLayout rlHomeContainer;
    @BindView(R.id.rb_home)
    RadioButton rbHome;
    @BindView(R.id.rb_game)
    RadioButton rbGame;
    @BindView(R.id.iv_center_navi)
    ImageView ivCenterNavi;
    @BindView(R.id.rb_find)
    RadioButton rbFind;
    @BindView(R.id.rb_mine)
    RadioButton rbMine;
    @BindView(R.id.rg_home)
    RadioGroup rgHome;
    @BindView(R.id.ll_main)
    RelativeLayout llMain;
    @BindView(R.id.fab)
    FloatingActionButton fab;
    @BindView(R.id.nav_view)
    NavigationView navView;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ButterKnife.bind(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        fab.hide();

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        setfargment();

        addFragment(list.get(0));
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }


    @OnClick({R.id.rb_home, R.id.rb_game, R.id.rb_find, R.id.rb_mine, R.id.iv_center_navi})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rb_home:
                showFragment(list.get(0));
                break;
            case R.id.rb_game:
                showFragment(list.get(1));
                break;
            case R.id.rb_find:
                showFragment(list.get(2));
                break;
            case R.id.rb_mine:
                showFragment(list.get(3));
                break;
            case R.id.iv_center_navi:
                Toast.makeText(MainActivity.this, "弹窗", Toast.LENGTH_SHORT).show();//这里也可用bottomsheet实现
                break;
        }
    }


    /**
     * 添加Fragment
     */
    private List<BaseFragment> list;

    private List<BaseFragment> setfargment() {
        list = new ArrayList<BaseFragment>();
        list.add(new HomeFragment());
        list.add(new GameFragment());
        list.add(new FindFragment());
        list.add(new MineFragment());
        return list;
    }


    /**
     * 显示fragment
     *
     * @param fragment
     */

    // TODO: 2018/8/14 这个方法有问题
    public void showFragment(BaseFragment fragment) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        hideAllFragments(ft);
        if (fragment == null) {
            throw new UnsupportedOperationException("the fragment you add is null");
        }
        if (!fragment.isAdded()) {
            ft.add(R.id.rl_home_container, fragment);
            fragment.onAttach(this);

        }
        ft.show(fragment);
        ft.commit();
    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param transaction 用于对Fragment执行操作的事务
     */
    private void hideAllFragments(FragmentTransaction transaction) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) != null) {
                transaction.hide(list.get(i));
            }
        }
    }


    //添加fragment
    protected void addFragment(BaseFragment fragment) {
        if (fragment != null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.rl_home_container, fragment, fragment.getClass().getSimpleName())
                    .addToBackStack(fragment.getClass().getSimpleName())
                    .commitAllowingStateLoss();
        }
        fragment.onAttach(this);
    }

}
